﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace docmgr.WinForm
{
    /// <summary>
    /// ycsk 2012-5-13
    /// UltraGrid表格空间自定义布局设置窗口
    /// 
    /// ycsk 2012-5-15 增加列对齐功能 和拖动行排序功能
    /// </summary>
    public partial class frmColumnSelect : Form
    {
        private System.Windows.Forms.DataGridView grid;
        private string owerFormName = "";
        DataTable dtAlign = new DataTable();

        public frmColumnSelect(System.Windows.Forms.DataGridView Grid, string formName)
        {
            InitializeComponent();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

            this.KeyPreview = true;

            this.grid = Grid;
            this.owerFormName = formName;

            dtAlign = new DataTable("dtAlign");
            dtAlign.Columns.Add("value", typeof(int));
            dtAlign.Columns.Add("caption", typeof(string));
            dtAlign.Rows.Add(new object[] { 0, "默认" });
            dtAlign.Rows.Add(new object[] { 1, "居左" });
            dtAlign.Rows.Add(new object[] { 2, "居中" });
            dtAlign.Rows.Add(new object[] { 3, "居右" });

            this.ColumnAlign.DataSource = dtAlign;
            this.ColumnAlign.ValueMember = "value";
            this.ColumnAlign.DisplayMember = "caption";

            this.dataGridView1.Rows.Clear();

            for (int i = 0; i < grid.Columns.Count; i++)
            {
                this.dataGridView1.Rows.Add(1);
                this.dataGridView1.Rows[i].Cells["ColumnName"].Value = grid.Columns[i].Name;
                this.dataGridView1.Rows[i].Cells["ColumnShow"].Value = grid.Columns[i].Visible;
                this.dataGridView1.Rows[i].Cells["ColumnTitle"].Value = grid.Columns[i].HeaderText;
                switch (grid.Columns[i].DefaultCellStyle.Alignment)
                {
                    case DataGridViewContentAlignment.NotSet:
                        this.dataGridView1.Rows[i].Cells["ColumnAlign"].Value = 0;
                        break;
                    case DataGridViewContentAlignment.MiddleLeft:
                        this.dataGridView1.Rows[i].Cells["ColumnAlign"].Value = 1;
                        break;
                    case DataGridViewContentAlignment.MiddleCenter:
                        this.dataGridView1.Rows[i].Cells["ColumnAlign"].Value = 2;
                        break;
                    case DataGridViewContentAlignment.MiddleRight:
                        this.dataGridView1.Rows[i].Cells["ColumnAlign"].Value = 3;
                        break;
                }
                this.dataGridView1.Rows[i].Cells["ColumnFormat"].Value = grid.Columns[i].DefaultCellStyle.Format;
                this.dataGridView1.Rows[i].Cells["ColumnPosition"].Value = grid.Columns[i].DisplayIndex;
            }
            this.dataGridView1.Sort(this.dataGridView1.Columns["ColumnPosition"], ListSortDirection.Ascending);
        }
       
        private void frmColumnSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (!e.Control) return;

            switch (e.KeyCode)
            {
                case Keys.S:
                    this.btnSave_Click(null, null);
                    break;
                case Keys.W: // 
                    this.tsbtnClose_Click(null, null);
                    break;
            }
        }

        private void tsbtnFormat_Click(object sender, EventArgs e)
        {
            if (ColumnFormat.Visible)
            {
                ColumnFormat.Visible = false;
                toolStripLabel1.Text = "[可以修改列标题和拖动行排序]";
            }
            else
            {
                ColumnFormat.Visible = true;
                toolStripLabel1.Text = "[输入合法字符格式化数据显示]";
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.dataGridView1.EndEdit();
            string cmname = "";

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                cmname = this.dataGridView1.Rows[i].Cells["ColumnName"].Value.ToString();

                if (this.dataGridView1.Rows[i].Cells["ColumnTitle"].Value == null || this.dataGridView1.Rows[i].Cells["ColumnTitle"].Value.ToString().Trim() == "")
                {
                    MessageBox.Show(this, "第" + (i + 1).ToString() + "列表头不能空！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                grid.Columns[cmname].Visible = (bool)this.dataGridView1.Rows[i].Cells["ColumnShow"].Value;
                grid.Columns[cmname].HeaderText = this.dataGridView1.Rows[i].Cells["ColumnTitle"].Value.ToString();
                switch (this.dataGridView1.Rows[i].Cells["ColumnAlign"].Value.ToString())
                {
                    case "0":
                        grid.Columns[cmname].DefaultCellStyle.Alignment = DataGridViewContentAlignment.NotSet;
                        break;
                    case "1":
                        grid.Columns[cmname].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                        break;
                    case "2":
                        grid.Columns[cmname].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                        break;
                    case "3":
                        grid.Columns[cmname].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        break;
                }
                if (this.dataGridView1.Rows[i].Cells["ColumnFormat"].Value != null)
                {
                    grid.Columns[cmname].DefaultCellStyle.Format = this.dataGridView1.Rows[i].Cells["ColumnFormat"].Value.ToString();
                }
                else
                {
                    grid.Columns[cmname].DefaultCellStyle.Format = "";
                }

                grid.Columns[cmname].DisplayIndex = i;
                this.dataGridView1.Rows[i].Cells["ColumnPosition"].Value = i;

                //Convert.ToInt32(this.dataGridView1.Rows[i].Cells["ColumnPosition"].Value.ToString());
            }
            //存取布局文件（，动态载入）
            if (System.IO.Directory.Exists(Common.AppPath + "config") == false)
                System.IO.Directory.CreateDirectory(Common.AppPath + "config");
            Common.SaveGridStyleToXML(owerFormName, grid);          

        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region  拖动行排序 [2012-5-15 ZhaoShunLu]
        // 拖动的源数据行索引
        private int indexOfItemUnderMouseToDrag = -1;
        // 拖动的目标数据行索引
        private int indexOfItemUnderMouseToDrop = -1;
        // 拖动中的鼠标所在位置的当前行索引
        private int indexOfItemUnderMouseOver = -1;
        // 不启用拖放的鼠标范围
        private Rectangle dragBoxFromMouseDown = Rectangle.Empty;

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            // 通过鼠标按下的位置获取所在行的信息
            var hitTest = dataGridView1.HitTest(e.X, e.Y);
            if (hitTest.Type != DataGridViewHitTestType.Cell)
                return;

            // 记下拖动源数据行的索引及已鼠标按下坐标为中心的不会开始拖动的范围
            indexOfItemUnderMouseToDrag = hitTest.RowIndex;
            if (indexOfItemUnderMouseToDrag > -1)
            {
                Size dragSize = SystemInformation.DragSize;
                dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
            }
            else
                dragBoxFromMouseDown = Rectangle.Empty;
        }
        private void dataGridView_MouseUp(object sender, MouseEventArgs e)
        {
            // 释放鼠标按键时清空变量为默认值
            dragBoxFromMouseDown = Rectangle.Empty;
        }
        private void dataGridView_MouseMove(object sender, MouseEventArgs e)
        {
            // 不是鼠标左键按下时移动
            if ((e.Button & MouseButtons.Left) != MouseButtons.Left)
                return;
            // 如果鼠标在不启用拖动的范围内
            if (dragBoxFromMouseDown == Rectangle.Empty || dragBoxFromMouseDown.Contains(e.X, e.Y))
                return;
            // 如果源数据行索引值不正确
            if (indexOfItemUnderMouseToDrag < 0)
                return;

            // 开始拖动，第一个参数表示要拖动的数据，可以自定义，一般是源数据行
            var row = dataGridView1.Rows[indexOfItemUnderMouseToDrag];
            DragDropEffects dropEffect = dataGridView1.DoDragDrop(row, DragDropEffects.All);

            //拖动过程结束后清除拖动位置行的红线效果
            OnRowDragOver(-1);
        }
        private void dataGridView_DragOver(object sender, DragEventArgs e)
        {
            // 把屏幕坐标转换成控件坐标
            Point p = dataGridView1.PointToClient(new Point(e.X, e.Y));

            // 通过鼠标按下的位置获取所在行的信息
            // 如果不是在数据行或者在源数据行上则不能作为拖放的目标
            var hitTest = dataGridView1.HitTest(p.X, p.Y);
            if (hitTest.Type != DataGridViewHitTestType.Cell || hitTest.RowIndex == indexOfItemUnderMouseToDrag)
            {
                e.Effect = DragDropEffects.None;
                OnRowDragOver(-1);
                return;
            }

            // 设置为作为拖放移动的目标
            e.Effect = DragDropEffects.Move;
            // 通知目标行重绘
            OnRowDragOver(hitTest.RowIndex);
        }
        private void dataGridView_DragDrop(object sender, DragEventArgs e)
        {
            // 把屏幕坐标转换成控件坐标
            Point p = dataGridView1.PointToClient(new Point(e.X, e.Y));

            // 如果当前位置不是数据行
            // 或者刚好是源数据行的下一行（本示例中假定拖放操作为拖放至目标行的上方）
            // 则不进行任何操作
            var hitTest = dataGridView1.HitTest(p.X, p.Y);
            if (hitTest.Type != DataGridViewHitTestType.Cell || hitTest.RowIndex == indexOfItemUnderMouseToDrag + 1)
                return;

            indexOfItemUnderMouseToDrop = hitTest.RowIndex;

            // * 执行拖放操作(执行的逻辑按实际需要)

            //var tempRow = dataTable.NewRow();
            //tempRow.ItemArray = dataTable.Rows[indexOfItemUnderMouseToDrag].ItemArray;
            //dataTable.Rows.RemoveAt(indexOfItemUnderMouseToDrag);

            //if (indexOfItemUnderMouseToDrag < indexOfItemUnderMouseToDrop)
            //    indexOfItemUnderMouseToDrop--;

            //dataTable.Rows.InsertAt(tempRow, indexOfItemUnderMouseToDrop);

            object[] dvrow = new object[6];
            for (int i = 0; i < dataGridView1.ColumnCount; i++)
            {
                dvrow[i] = dataGridView1.Rows[indexOfItemUnderMouseToDrag].Cells[i].Value;
            }

            dataGridView1.Rows.RemoveAt(indexOfItemUnderMouseToDrag);
            if (indexOfItemUnderMouseToDrag < indexOfItemUnderMouseToDrop)
                indexOfItemUnderMouseToDrop--;
            dataGridView1.Rows.Insert(indexOfItemUnderMouseToDrop, dvrow);
        }

        private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            // 如果当前行是鼠标拖放过程的所在行
            if (e.RowIndex == indexOfItemUnderMouseOver)
                e.Graphics.FillRectangle(Brushes.Red, e.RowBounds.X, e.RowBounds.Y, e.RowBounds.Width, 2);
        }
        private void OnRowDragOver(int rowIndex)
        {
            // 如果和上次导致重绘的行是同一行则无需重绘
            if (indexOfItemUnderMouseOver == rowIndex)
                return;

            int old = indexOfItemUnderMouseOver;
            indexOfItemUnderMouseOver = rowIndex;

            // 去掉原有行的红线
            if (old > -1)
                dataGridView1.InvalidateRow(old);

            // 绘制新行的红线
            if (rowIndex > -1)
                dataGridView1.InvalidateRow(rowIndex);
        }


        #endregion

    }
}
